#Academic Search Complete     Business Source Complete
import requests
import re
import pymysql
import json
import time
import re
from bs4 import BeautifulSoup
from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
proxies = {
            "http": "http://qf:qf3.1415@202.112.118.7:10086/"
        }


def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def updateCookie(cookies,tempCookies):

    for k, v in tempCookies.items():
        cookies[k] = v

    return cookies


def first():

    headers = {
        'Origin': 'http://search.ebscohost.com',
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36',
        'Referer': 'http://search.ebscohost.com/Community.aspx?community=y&authtype=ip&stsug=AjSxQzTefuCWINPnN0ed2BYtzUyJ43MMLU4ZA4Q6h8xsP4PgEXCSVO7GSUrVydRL5MTf1PbvjR1kQHAxxLafP2hBuKT8rx34yjfLPfHMmNGHlXmOCJ8Ak_bP4k6ZUqRHf6VDjEZNTEONJ-as9mx8xNSj1iiA86kWzM1oEDxVFUsQ1Rc&IsAdminMobile=N&encid=22D731263C3635973766359632653C37380376C376C376C372C374C371C376C33013',
    }

    params = (
        ('community', 'y'),
        ('authtype', 'ip'),
        ('encid', '22D731263C3635973766359632653C37380376C376C376C372C374C371C376C33013'),
        ('IsAdminMobile', 'N'),
        ('authpid', 'ascbsc'),
    )

    data = {
        '__EVENTTARGET': '_ctl10',
        '__EVENTARGUMENT': '',
        '__VIEWSTATE': 'au0RKRJ7u6j6HmAQH986J6Ve9KNi6g7mYiGSNz2r8bbe1i6RtV+IG8s8s7KivkvVPqJ9GU/Rx0PRQCB7pWweduatJuiFbFJ1w1ZiRa6Rj/0O+9Mp0yAKd8tVaUhQN3NN84FuwA==',
        '__PREVIOUSPAGE': '832syxT-Ep5f4bn61m4dqKaB8KV6q17Xxmls84BLgBdcOMGbkJPXdxhR-oW5noPkq3oD4Ir7I0MnVIVoFRu70AfeE3g1',
        '__EVENTVALIDATION': 'CYQmIPSwOGqK4CpfgxYtl5qFhXPdNVxEmRFldYlTrXPjKo8VAxiYLNdDzrO1T3wgBtcNYvGrMA3plkaaj9SXMHpQLhXso91CX+7cPYkMj0cMHntoBqqgZ3sks6D8U2o9J8M3Y+cSjw6q6tdaIQnesz044TK6+86l+fVlRZiiFoXpRJKgHscyWjItTjxv8sAD2pOJoEG3j+RAhP3PXtxGbx6f+SyCpPF3yoFpykjWDFaWnMU6bamyZ2mXMPUk2RYbzlq9lCGCRnh5qjw55hhhdI2PgG9zl/5sH2BkNSp4i7bnsgN0uGPmU+6RpQ8IVJZ+HCkouQ==',
        'cpToken': '723731163C1635973766352632453E9228E366D36613669362E324E331133603',
        'selectServicesToken': ''
    }


    s = requests.session()
    response = s.post('http://search.ebscohost.com/Community.aspx', headers=headers, params=params , data=data,proxies = proxies)

    cookies = s.cookies.get_dict()


    return cookies



# exit()
def second(cookies,sid):
    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Referer': 'http://web.a.ebscohost.com/ehost/search/selectdb?vid=0&sid={}'.format(sid),
        'Origin': 'http://web.a.ebscohost.com',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    }

    params = (
        ('vid', '0'),
        ('sid', sid),
    )

    s = requests.session()
    response = s.get('http://web.a.ebscohost.com/ehost/search/selectdb', headers=headers, params=params, cookies=cookies,proxies = proxies)


    return s.cookies.get_dict()

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")


    lis = soup.select(".result-list-li")

    page_number1 = soup.select(".content-header h1")[0].text
    px = re.compile("共 (.*?) 个")
    allNum = px.findall(page_number1)[0].strip().replace(",", '')

    for li in lis:
        tempData = resultData()
        try:
            tempData["titleName"] = li.select("h3 > a")[0].text
        except:
            pass
        try:
            tempData["titleHref"] = li.select("h3 > a")[0]["href"]
        except:
            pass
        try:
            tempData["abstract"] = li.select(".abstract")[0].text
        except:
            pass
        allData.append(tempData)
    return allData,allNum

#得到搜索页面
def getSearchWeb(cookies,sid):

    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Origin': 'http://web.a.ebscohost.com',
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://web.a.ebscohost.com/ehost/search/selectdb?vid=0&sid=c352310c-6647-44e6-b7df-02db62deb3ef%40sdc-v-sessmgr03',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('vid', '0'),
        ('sid', sid),
    )

    data = {
        'RelRequestPath': 'search/selectdb',
        '__EVENTTARGET': '',
        '__EVENTARGUMENT': '',
        '__sid': sid,
        '__vid': '0',
        '__CUSTOMVIEWSTATE': 'H4sIAAAAAAA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qFlgrb98hEvTBOrubPnJIFQJleGJNXz/gVZYw6PPaX9v7wkab7wsZ35Xi+MQQl8+9xUNmGrx1ZvSqF6cx0vLlaEVPDpSZe5hBuqqDG1WMhl9agXfWdCnFiClNpkDNDP6YQmXJt6UxgBPLT7GE+O0v6unTqN5flJpLQpP3CinhyC5ns0DpVztvvDGjaoqxWJI/l917mqhrGGFd+3jXPupjjMpWYgjWab7pVSSRXVFuVprcna1mf4HKDv8NCjYdXDv8N3q/csC6t3L+V31avNDeGWwLlT9RRFPj6jrc436VDcWiP/GE/1+Hhpnkg6dxfkNErg1Whc/vGovMkREf9zUCpNAnLqZU9cLjUI0VUgCjTtiyh5e1fW02VSi9PwQnvwN0pKb9ctT6Ep5e3NHHseJPAhWLl3J3AYV7S6+1s5dYmu8G+Q+GNCAAlrofr8Wfk3v0oLn/Uo4sPvjd7DhU0uebTsdsjdMlNZvmGtCFL7bZCZ8Wp0JciqRkJKKORiMgWDMG+NTbHXYG7Ia083mtPFJFKF8JFzeHW/MCJLDnBuJpHCNYa97eCUmOvfB5GTcTEqRqM8ikgQjJFTdDyUkUYAfUYgPkAF3qKmtMjepLw9qU9v66kxN+js1uD6iPP8FHzBisoHu+pjqqIIaxpAUCaVk1Aipbm7DfdbmF7VnW1x9wDRA9n4Y5FFWSlxPClFeAfBDVfC6rQHZDp8ruBk7E9O8gG2fH/UfD35BB3z+IA092V9raau533nqsNtNwyGSJ2kZRvbxQE/QE4N/dTyMzEJlNO12n+b+Bnt/duDMCQAA',
        '__ScreenResolution': '1440 : 900',
        '__VIEWSTATE': '',
        'ctl00$ctl00$MainContentArea$MainContentArea$continue1': '\u7EE7\u7EED',
        'ctl00$ctl00$MainContentArea$MainContentArea$selectAll': 'on',
        'ctl00$ctl00$MainContentArea$MainContentArea$SelectDbControl$dbList$ctl01$itemCheck': 'a9h',
        'ctl00$ctl00$MainContentArea$MainContentArea$SelectDbControl$dbList$ctl02$itemCheck': 'bth',
        'ajax': 'enabled'
    }


    s = requests.session()
    response = s.post('http://web.a.ebscohost.com/ehost/search/selectdb', headers=headers, params=params,cookies=cookies, data=data,proxies = proxies)

    cookies = s.cookies.get_dict()

    return cookies

def getDataOnePage(keyword,cookies,sid):
    # import requests


    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Origin': 'http://web.a.ebscohost.com',
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://web.a.ebscohost.com/ehost/resultsadvanced?vid=3&sid=59801a8e-c1b6-4602-b01a-1c1a3b429ec6%40sdc-v-sessmgr05&bquery=TI+children&bdata=JmRiPWE5aCZkYj1idGgmbGFuZz16aC1jbiZ0eXBlPTEmc2l0ZT1laG9zdC1saXZl',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('sid', sid),
        ('vid', '1'),
    )

    data = [
        ('RelRequestPath', 'search/advanced'),
        ('__EVENTTARGET', ''),
        ('__EVENTARGUMENT', ''),
        ('__sid', sid),
        ('__vid', '1'),
        ('__CUSTOMVIEWSTATE',
         'H4sIAAAAAAAEAJWRP08bMRjGFSdOAglkQImAITkEYrtLLoqiBhBLJ4Z2qCjryZydnMXd+Tj7ItINscDGEDEgZWCiA3wEypepEvVTUPt6aU90Acmyrcevn9/75yVTwRVY0s12W61Op1upgfrKEXIpRoJ8IacR4eITwwRkaqBfwckGMiou5yJ/AOE3R7d9qWWVBm0XcQ5rPtP5CfU1hIfItwnWiMO4kFFQ/i/OTdSPwhHl9NglDsagqJRGVl2X/oX8EZYTYb6XY6B6A/mUXekzGtKBTP5r6MI1R4hgp9nkURCwUBjkmNvMsJnXlIaFONzaOBDE+8giX4BsLZ8CxFXm6jknJH3YUk78tZWqKbaLq2sGIR0ie2Q4wnPrC7KDSEeRYHC9z5ggoZW8WwFzqTxc6p+ohlYz5V+317P78+n4efZ4gwF4lUMjD4238CXD4zG9UYCrCTQWLda3Ik7SyJ9Pz7Px9+nkbvrwA8te/of88D7kps2CUUgHjpDwagL/q6XI+dnV5XRygcFCamy5Q3ImYHl70+z2drV2y+xpckSL8wmX1KUIt/Y2dF2ThYQ7GjfNXrfd6Rkeor6BuC2z0zRd38e/ARAGPIHWAgAA'),
        ('__VIEWSTATE', ''),
        ('GuidedSearchFormData.Index', '1'),
        ('GuidedSearchFormData.Index', '2'),
        ('GuidedSearchFormData.Index', '3'),
        ('GuidedSearchFormData[1].SearchTerm', keyword),
        ('GuidedSearchFormData[1].DbTag', ''),
        ('GuidedSearchFormData[2].Op', 'AND'),
        ('GuidedSearchFormData[2].SearchTerm', ''),
        ('GuidedSearchFormData[2].DbTag', ''),
        ('GuidedSearchFormData[3].Op', 'AND'),
        ('GuidedSearchFormData[3].SearchTerm', ''),
        ('GuidedSearchFormData[3].DbTag', ''),
        ('PerformSearchSettingValue', '3'),
        ('searchMode', 'Bool'),
        ('common_DT1', ''),
        ('common_DT1_FromYear', ''),
        ('common_DT1_ToMonth', ''),
        ('common_DT1_ToYear', ''),
        ('common_SO', ''),
        ('common_PT83', ''),
        ('common_PZ7', ''),
        ('common_LA99', ''),
        ('common_PG4', ''),
        ('common_PG4_NumVal', ''),
        ('common_PS', ''),
        ('common_IC', ''),
        ('common_CO', ''),
        ('common_DN', ''),
        ('common_TK', ''),
        ('ajax', 'enabled'),
    ]
    s = requests.session()
    response = s.post('http://web.a.ebscohost.com/ehost/Search/PerformSearch', headers=headers, params=params,
                             cookies=cookies, data=data,proxies = proxies)

    result, allNum = formData(response)
    cookies = s.cookies.get_dict()
    return cookies, result, allNum



    # NB. Original query string below. It seems impossible to parse and
    # reproduce query strings 100% accurately so the one below is given
    # in case the reproduced version is not "correct".
    # response = requests.post('http://web.a.ebscohost.com/ehost/Search/PerformSearch?sid=59801a8e-c1b6-4602-b01a-1c1a3b429ec6@sdc-v-sessmgr05&vid=1', headers=headers, cookies=cookies, data=data)
def getMorePageData(dd,cookies,startYear,endYear,sid,page=2):


    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Origin': 'http://web.a.ebscohost.com',
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        # 'Referer': 'http://web.a.ebscohost.com/ehost/resultsadvanced?vid=1&sid={}&bquery=TI+children&bdata=JmRiPWE5aCZkYj1idGgmbGFuZz16aC1jbiZ0eXBlPTEmc2l0ZT1laG9zdC1saXZl'.format(sid),
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('vid', '3'),
        ('sid', sid),

    )

    data = [
        ('RelRequestPath', 'resultsadvanced'),
        ('__EVENTTARGET', 'ctl00$ctl00$MainContentArea$MainContentArea$bottomMultiPage$rptPageLinks$ctl0{}$lnkPageLink'.format(page - 1)),
        ('__EVENTARGUMENT', ''),
        ('__sid', sid),
        ('__vid', '3'),
        ('__CUSTOMVIEWSTATE',
         'H4sIAAAAAAAEAM1Y3W8bxxEvKa5ESVaYIJEaNwhJK0ZkFSF53+TJdQFZqRsntRu4il+J5e0eedHxjr07ylY/ACepW8cwKqNJWzdW4ro1ihSB7X4EaWC7yR8TH628NOhLX9PZvSNFqkoiQ3ZSgjjeLmdnZmd+M7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F02FUkoQ5NpUm0bOO2mW2mHOBW2gdOhAZyev0c4VXUsC6aimYJo6LpGBFXWTFEiNQGOGhVTGTRCnQbBSh9SVXZwlyX1qwKpshOQ/utLAam8M5BKZbmsKYIkm7VyRTFN2STgH2wQQcJiBUtb+YfDtN81IFfeAUIlXdDkbSGUNSqPoPQh8M4R16Pgu8e38B0B8DTpAWDNe/++2wfWj/BjCW4HLnqI3T5U2W1A1afQaLjdG6KNTmZisDcjyQcyg+3Lro22eriPNo12x57w262W68VO4MYnUXMaddR9rLprx5DA1vqbF7Nbqch77K3U8qxlbKwUG0HT7uvD+vkUt8MHgNT0u1yGtuBSuTsuTxhua4W3vn03VXEj9eQToqbvz0MDXsmT7kVEI26gU2jvt/YUCnk4UnpzeV8UdU1S9GITgFCEvhSE5vOFwrfJfwHR7J2AthUAAA=='),
        ('__VIEWSTATE', ''),
        # ('GuidedSearchFormData.Index', '1'),
        # ('GuidedSearchFormData.Index', '2'),
        # ('GuidedSearchFormData.Index', '3'),
        # ('GuidedSearchFormData[1].SearchTerm', 'children'),
        # ('GuidedSearchFormData[1].DbTag', 'TI'),
        # ('GuidedSearchFormData[2].Op', 'AND'),
        # ('GuidedSearchFormData[2].SearchTerm', ''),
        # ('GuidedSearchFormData[2].DbTag', ''),
        # ('GuidedSearchFormData[3].Op', 'AND'),
        # ('GuidedSearchFormData[3].SearchTerm', ''),
        # ('GuidedSearchFormData[3].DbTag', ''),
        ('PerformSearchSettingValue', '2'),
        ('ctl00$ctl00$Column1$Column1$ctl00$ctrlResultsDualSliderDate$txtFilterDateFrom', '1898'),
        ('ctl00$ctl00$Column1$Column1$ctl00$ctrlResultsDualSliderDate$txtFilterDateTo', '2019'),
        ('ctl00$ctl00$Column1$Column1$ctl00$ctrlResultsDualSliderDate$MinDateOrig', '1898'),
        ('ctl00$ctl00$Column1$Column1$ctl00$ctrlResultsDualSliderDate$MaxDateOrig', '2019'),
        ('ctl00$ctl00$Column1$Column1$ctl00$isSliderChanged', '0'),
        ('ctl00$ctl00$Column1$Column1$ctl00$HasSliderBeenSet', '0'),
        ('common_DT1_FromYear', startYear),
        ('common_DT1_ToYear', endYear),
        ('_sort_', 'Hits'),
        ('_sort_', 'Hits'),
        ('_sort_order_', 'Desc'),
        ('_sort_order_', 'Desc'),
        ('_clusterId_', 'SubjectThesaurus'),
        ('_clusterId_', 'Subject'),
        ('_clusterId_', 'Journal'),
        ('_clusterId_', 'Publisher'),
        ('_clusterId_', 'SubjectCompany'),
        ('_clusterId_', 'Language'),
        ('_clusterId_', 'SubjectGeographic'),
        ('_clusterId_', 'SubjectNAICS'),
        ('QuickSearchField', ''),
        ('QuickSearchViewData',
         '{"Caption":"\u516C\u53F8","Command":"companyinforesults","SearchFieldName":"QuickSearchText","ShortDbName":"cix","IsLimiter":false,"SearchTag":"CO","WaterMark":"Enter company name","IsVisible":true}'),
        ('ajax', 'enabled'),
    ]
    data.extend(dd)


    s = requests.session()
    response = s.post('http://web.a.ebscohost.com/ehost/resultsadvanced', headers=headers, params=params,
                             cookies=cookies, data=data,proxies = proxies)
    cookies = s.cookies.get_dict()


    result,allNum = formData(response)
    return cookies,result,allNum
    # NB. Original query string below. It seems impossible to parse and
    # reproduce query strings 100% accurately so the one below is given
    # in case the reproduced version is not "correct".
    # response = requests.post('http://web.a.ebscohost.com/ehost/resultsadvanced?vid=9&sid=59801a8e-c1b6-4602-b01a-1c1a3b429ec6%40sdc-v-sessmgr05&bquery=TI+children&bdata=JmRiPWE5aCZkYj1idGgmbGFuZz16aC1jbiZ0eXBlPTEmc2l0ZT1laG9zdC1saXZl', headers=headers, cookies=cookies, data=data)

    # NB. Original query string below. It seems impossible to parse and
    # reproduce query strings 100% accurately so the one below is given
    # in case the reproduced version is not "correct".
    # response = requests.post('http://web.a.ebscohost.com/ehost/resultsadvanced?vid=8&sid=59801a8e-c1b6-4602-b01a-1c1a3b429ec6%40sdc-v-sessmgr05&bquery=TI+children&bdata=JmRiPWE5aCZkYj1idGgmbGFuZz16aC1jbiZ0eXBlPTEmc2l0ZT1laG9zdC1saXZl', headers=headers, cookies=cookies, data=data)

def getZ():
    # 选取类型
    cookies = first()

    EHost2 = cookies["EHost2"]
    px = re.compile("sid=(.*?)&")
    sid = px.findall(EHost2)[0]

    # 选取数据库
    tempCookies = second(cookies, sid)
    cookies = updateCookie(cookies, tempCookies)

    # 进入到高级搜索界面
    tempCookies = getSearchWeb(cookies, sid)
    cookies = updateCookie(cookies, tempCookies)
    return cookies

def index(request):

    page = 1
    data = {"values": ["cell","c"], "type": ["TI","AU"], "log": ["and"]}

    if page == 1:

        # print(dd)
        cookies = getZ()

        EHost2 = cookies["EHost2"]
        px = re.compile("sid=(.*?)&")
        sid = px.findall(EHost2)[0]

        tempCookies, allData,allNum = getDataOnePage(data["values"][0],cookies,sid)
        return HttpResponse(json.dumps({
            "collection": allData,
            "allNum": 0
        }), content_type="application/json")
